package com.novoda.runbuddy.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import android.util.Log;

import com.novoda.runbuddy.activity.stopwatch.StopwatchTools;

public class DBHelperTools {

	private static final String	TAG	= "DBHelperTools";

	// Translate a long into the ISO-8601 standard
	public static String isoFormat(long date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
		GregorianCalendar calendar = new GregorianCalendar();

		calendar.setTimeInMillis(date);
		return dateFormat.format(calendar.getTime());
	}

	public static long expectedFormat(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
		GregorianCalendar calendar = new GregorianCalendar();

		try {
			calendar.setTime(dateFormat.parse(date));
		} catch (ParseException e) {
			Log.e(TAG, "Wrongly formated date - not expected in this function");
			e.printStackTrace();
		}
		return calendar.getTimeInMillis();
	}

	public static long transformDBFormattedDateIntoMilliSecs(String dateTimeReturnedFromDB) {

		try {
			dateTimeReturnedFromDB = dateTimeReturnedFromDB.substring(11);
		} catch (StringIndexOutOfBoundsException exp) {
			Log.e(TAG, "Tried to extract time from a wrongly formatted String");
		}

		return StopwatchTools.getMilliSecs(dateTimeReturnedFromDB);
	}

}